
*note: set working directory below
* cd

use "data.dta", clear

* setup
set matsize 11000
encode province, gen(id)
xtset id year

******************************************************************
* Table 3: Estimates of the effect of proportionality on turnout
******************************************************************

* 1) baseline
xtreg turnout isproportional i.year, fe robust
outreg2  using "Table3.tex", tex nocon replace dec(3)  keep(isproportional)
boottest isproportional, seed(1)

* 2) controlling for magnitude
xtreg turnout isproportional magnitud  i.year, fe robust
outreg2  using "Table3.tex", tex nocon append dec(3)  keep(isproportional magnitud)
boottest isproportional, seed(1)

* 3) DID estimates 

* de Chaisemartin and D'Haultfoeuille (2020) diagnostic - weights (footnote)
twowayfeweights turnout id year isproportional, type(feTR) summary_measures


* DID_M
did_multiplegt turnout id year isproportional, cluster(id) breps(1000) seed(1)

* DID_l
did_multiplegt turnout id year isproportional, cluster(id) robust_dynamic breps(1000) seed(1)

* Placebo (parallel trends)
did_multiplegt turnout id year isproportional, cluster(id) breps(1000) placebo(1) seed(1)

* (footnote) replace turnout in Santiago del Estero in 1918 (election declared void)
gen turnout_robustness = turnout
replace turnout_robustness = 19.03 if province=="Santiago del Estero" & year==1918
xtreg turnout_robustness isproportional i.year, fe robust
did_multiplegt turnout_robustness id year isproportional, cluster(id) breps(1000) seed(1)


******************************************************************
* Table 4 (mechanisms): Changes in patterns of party competition
******************************************************************


* 1) The number of parties
xtreg enp isproportional i.year, fe robust
outreg2  using "Table4.tex", tex nocon replace dec(3)  keep(isproportional)
boottest isproportional, seed(1)
did_multiplegt enp id year isproportional, cluster(id) breps(1000) seed(1)
did_multiplegt enp id year isproportional, cluster(id) robust_dynamic breps(1000) seed(1)


* 2) Party splinters
xtreg splinter isproportional i.year, fe robust
outreg2  using "Table4.tex", tex nocon append dec(3)  keep(isproportional)
boottest isproportional, seed(1)
did_multiplegt splinter id year isproportional, cluster(id) breps(1000) seed(1)
did_multiplegt splinter id year isproportional, cluster(id) robust_dynamic breps(1000) seed(1)

* (footnote) switchers-in vs switchers-out
did_multiplegt splinter id year isproportional, cluster(id) breps(1000) seed(1) switchers(in)
did_multiplegt splinter id year isproportional, cluster(id) breps(1000) seed(1) switchers(out)

* 3) Competitiveness I
xtreg first_vs_second isproportional i.year, fe robust
outreg2  using "Table4.tex", tex nocon append dec(3)  keep(isproportional)
boottest isproportional, seed(1)
did_multiplegt first_vs_second id year isproportional, cluster(id) breps(1000) seed(1)
did_multiplegt first_vs_second id year isproportional, cluster(id) robust_dynamic breps(1000) seed(1)

* 4) Competitiveness II
xtreg first_percent isproportional i.year, fe robust
outreg2  using "Table4.tex", tex nocon append dec(3)  keep(isproportional)
boottest isproportional, seed(1)
did_multiplegt first_percent id year isproportional, cluster(id) breps(1000) seed(1)
did_multiplegt first_percent id year isproportional, cluster(id) breps(1000) robust_dynamic seed(1)

* 5) Competitiveness III
xtreg minimum_share isproportional i.year, fe robust
outreg2  using "Table4.tex", tex nocon append dec(3)  keep(isproportional)
boottest isproportional, seed(1)
did_multiplegt minimum_share id year isproportional, cluster(id) breps(1000) seed(1)
did_multiplegt minimum_share id year isproportional, cluster(id) breps(1000) robust_dynamic seed(1)


**************
* APPENDIX B * 
**************

* cross sectional results
foreach x in lijphart_index max_dev_index rae_index loosmore_hanby_index grofman_index gallagher_index monroe_index ryabtsev_index szalai_index {
reg `x' isproportional, cluster(id)
outreg2  using "AppendixB1.tex", tex nocon append dec(3)  keep(isproportional)
}

* DID results
foreach x in lijphart_index max_dev_index rae_index loosmore_hanby_index grofman_index gallagher_index monroe_index ryabtsev_index szalai_index {
xtreg `x' isproportional i.year, fe robust
outreg2  using "AppendixB2.tex", tex nocon append dec(3)  keep(isproportional)
}

**************
* APPENDIX C * 
**************

foreach x in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15{
xtreg turnout isproportional i.year if id!=`x', fe robust
outreg2  using "AppendixC.tex", tex nocon append dec(3)  keep(isproportional)
}


**************
* APPENDIX E * 
**************

xtreg turnout i.isproportional##c.avg_mmp_share1st i.year, fe robust
outreg2  using "AppendixE.tex", tex nocon append dec(3)  keep(i.isproportional i.isproportional##c.avg_mmp_share1st)

lincom _b[1.isproportional] + 0.5*_b[1.isproportional#c.avg_mmp_share1st]
lincom _b[1.isproportional] + 0.6*_b[1.isproportional#c.avg_mmp_share1st]
lincom _b[1.isproportional] + 0.7*_b[1.isproportional#c.avg_mmp_share1st]
lincom _b[1.isproportional] + 0.8*_b[1.isproportional#c.avg_mmp_share1st]
